﻿using BJYJ_Admin.Core.Utils.ResultUtils;

namespace BJYJ_Admin.Application.System.Services.User;
[ApiDescriptionSettings("System")]

public class SysLogService : IDynamicApiController, ITransient
{
    public readonly ISqlSugarClient _db;
    public SysLogService(ISqlSugarClient db)
    {
        _db = db;
    }
    /// <summary>
    /// 获取日志列表信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("获取日志列表信息")]
    [NoEncrypt]
    public virtual async Task<PageResult<LogListOutDto>> GetLogList([FromQuery] LogSearchDto input)
    {
        RefAsync<int> totalCount = 0;
        var Level = "";
        switch (input.TYPE)
        {
            case 0:
                Level = "GetUserInfo";
                break;
            case 1:
                Level = "";
                break;
            case 2:
                Level = "Information";
                break;
            case 3:
                Level = "Error";
                break;
            default:
                Level = "";
                break;
        }
        var log = await _db.Queryable<SYS_LOG>().
                    WhereIF(!string.IsNullOrWhiteSpace(input.USERNAME), x => x.USERNAME.Contains(input.USERNAME))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.IP), x => x.REMOTEIPV4.Contains(input.IP))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.ACTIONTYPENAME), x => x.ACTIONTYPENAME.Contains(input.ACTIONTYPENAME))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.DISPLAYTITLE), x => x.DISPLAYTITLE.Contains(input.DISPLAYTITLE))
                    .WhereIF(input.TYPE.HasValue,
                      input.TYPE == 0 ? x => x.ACTIONTYPENAME == Level : x => x.LOGLEVEL.Contains(Level))
                    .OrderBy(x => x.ID, OrderByType.Desc)
                    .Select(outLog => new LogListOutDto
                    {
                        ID = outLog.ID,
                        USERNAME = outLog.USERNAME,
                        ACTIONNAME = outLog.ACTIONNAME,
                        BROWSER = outLog.BROWSER,
                        ACTIONTYPENAME = outLog.ACTIONTYPENAME,
                        DISPLAYTITLE = outLog.DISPLAYTITLE,
                        REMOTEIPV4 = outLog.REMOTEIPV4,
                        HTTPMETHOD = outLog.HTTPMETHOD,
                        TRACEID = outLog.TRACEID,
                        THREADID = outLog.THREADID,
                        REQUESTURL = outLog.REQUESTURL,
                        TIMEOPERATIME = outLog.TIMEOPERATIME,
                        LOGLEVEL = outLog.LOGLEVEL,
                        CREATE_TIME = outLog.CREATE_TIME
                    })
                    .ToPageListAsync(input.page, input.limit, totalCount);
        return new PageResult<LogListOutDto>
        {
            page = input.page,
            limit = input.limit,
            totalcount = totalCount,
            data = log
        };
    }

    /// <summary>
    /// 日志详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [DisplayName("日志详情")]
    public virtual async Task<SYS_LOG> GetLogInfoByID(int id)
    {
        var user = await _db.Queryable<SYS_LOG>().Where(x => x.ID == id).FirstAsync();
        return user;
    }

}

